
<template>
    <div class="layout">
        <div v-if="!login">
         <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
            <FormItem prop="user">
                <Input type="text" v-model="formInline.user" placeholder="Username">
                    <Icon type="ios-person-outline" slot="prepend"></Icon>
                </Input>
            </FormItem>
            <FormItem prop="password">
                <Input type="password" v-model="formInline.password" placeholder="Password">
                    <Icon type="ios-locked-outline" slot="prepend"></Icon>
                </Input>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
            </FormItem>
        </Form>
        </div>
        <div v-else>
        <Layout>
            <Header>
                <Menu mode="horizontal" theme="dark" active-name="1">
                    <div class="layout-logo"></div>
                    <div class="layout-nav">
                        <MenuItem name="1">
                            <Icon type="ios-navigate"></Icon>
                            <router-link tag='span' to='/users'>
                                      用户管理
                                </router-link>
                        </MenuItem>
                        <MenuItem name="2">
                            <Icon type="ios-keypad"></Icon>
                            <router-link tag='span' to='/cate'>
                                      分类管理
                                </router-link>
                        </MenuItem>
                        <MenuItem name="3">
                            <Icon type="ios-analytics"></Icon>
                            <router-link tag='span' to='/new'>
                                      新闻管理
                                </router-link>
                        </MenuItem>
                        <MenuItem name="4">
                            <Icon type="ios-paper"></Icon>
                            <router-link tag='span' to='/product'>
                                      产品管理
                                </router-link>
                        </MenuItem>
                         <MenuItem name="5">
                            <Icon type="ios-upload"></Icon>
                            <router-link tag='span' to='/upload'>
                                      上传管理
                                </router-link>
                        </MenuItem>
                    </div>
                </Menu>
            </Header>
            <Layout>
                <Sider hide-trigger :style="{background: '#fff'}">
                    <Menu active-name="1-2" theme="light" width="auto" :open-names="['1']">
                        <Submenu name="1">
                            <template slot="title">
                                <Icon type="ios-navigate"></Icon>
                                <router-link tag='span' to='/users'>
                                      用户管理
                                </router-link>
                            </template>
                            <!-- <MenuItem name="1-1">Option 1</MenuItem>
                            <MenuItem name="1-2">Option 2</MenuItem>
                            <MenuItem name="1-3">Option 3</MenuItem> -->
                        </Submenu>
                        <Submenu name="2">
                            <template slot="title">
                                <Icon type="ios-keypad"></Icon>
                                <router-link tag='span' to='/cate'>
                                      分类管理
                                </router-link>
                            </template>
                            <!-- <MenuItem name="2-1">Option 1</MenuItem>
                            <MenuItem name="2-2">Option 2</MenuItem> -->
                        </Submenu>
                        <Submenu name="3">
                            <template slot="title">
                                <Icon type="ios-analytics"></Icon>
                                <router-link tag='span' to='/new'>
                                      新闻管理
                                </router-link>
                            </template>
                            <!-- <MenuItem name="3-1">Option 1</MenuItem>
                            <MenuItem name="3-2">Option 2</MenuItem> -->
                        </Submenu>
                          
                        <Submenu name="4">
                            <template slot="title">
                                <Icon type="ios-upload"></Icon>
                                <router-link tag='span' to='/upload'>
                                      上传管理
                                </router-link>
                            </template>
                            <!-- <MenuItem name="4-1">Option 1</MenuItem>
                            <MenuItem name="4-2">Option 2</MenuItem> -->
                        </Submenu>
                    </Menu>
                </Sider>
                <Layout >
                    <!-- <Breadcrumb :style="{margin: '24px 0'}">
                        <BreadcrumbItem>Home</BreadcrumbItem>
                        <BreadcrumbItem>Components</BreadcrumbItem>
                        <BreadcrumbItem>Layout</BreadcrumbItem>
                    </Breadcrumb> -->
                    <Content :style="{padding: '24px', minHeight: '480px', background: '#fff'}">
                        <router-view></router-view>
                    </Content>
                </Layout>
            </Layout>
        </Layout>
    </div>
    </div>
</template>
<script>
import md5 from 'crypto-js/md5';
import {mapGetters,mapActions} from 'vuex';
    export default {
  data () {
            return {
                isCollapsed: false,
                 formInline: {
                    user: '',
                    password: ''
                },
                ruleInline: {
                    user: [
                        { required: true, message: 'Please fill in the user name', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: 'Please fill in the password.', trigger: 'blur' },
                        { type: 'string', min: 6, message: 'The password length cannot be less than 6 bits', trigger: 'blur' }
                    ]
                }
            };
        },
        methods:{
            ...mapActions(['checkLogin']),
             handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        
                        this.axios({
                        method:'post',
                        url:`http://10.31.155.74:3000/users/list`,
                        data:this.filter
                    }).then((response) => {
                        console.log(response)
                        
                        for(var i=0;i<response.data.rows.length;i++){
                            if(response.data.rows[i].name == this.formInline.user && response.data.rows[i].password == this.formInline.password ){
                                this.checkLogin(true);
                            }
                        }
                    })

                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            }
        
        },
        computed: {
            ...mapGetters(['login']),
            menuitemClasses: function () {
                return [
                    'menu-item',
                    this.isCollapsed ? 'collapsed-menu' : ''
                ]
            }
        }
    }
</script>

<style scoped>

.layout{
    border: 1px solid #d7dde4;
    background: #f5f7f9;
    position: relative;
    border-radius: 4px;
    overflow: hidden;
}
.layout-logo{
    width: 100px;
    height: 30px;
    background: #5b6270;
    border-radius: 3px;
    float: left;
    position: relative;
    top: 15px;
    left: 20px;
}
.layout-nav{
    width: 500px;
    margin: 0 auto;
    margin-right: 20px;
}

</style>